/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.saber.certificacao.filtros;

import br.com.saber.certificacao.beansjsf.login.LoginManager;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Garante que apenas professor tenha acesso ao menu cadastro
 * @author gbvbahia
 */
public class FilterCadastro implements Filter{
    
    private FilterConfig filterConfig = null;
    
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
         HttpServletRequest rq = (HttpServletRequest) request;
        HttpServletResponse rs = (HttpServletResponse) response;
        HttpSession session = rq.getSession();
        Integer level = (Integer) session.getAttribute(LoginManager.SESSION_NIVEL);
        if ( level == null || level < 50) {
            rs.sendRedirect(session.getServletContext().getContextPath() + "/certificacao/principal.xhtml");
        }else {
            chain.doFilter(rq, rs);
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }
    
}
